package cn.hn3l.agriculture.services.servicesImplements.goods;

import cn.hn3l.agriculture.dao.mapper.datum.EffectMapper;
import cn.hn3l.agriculture.dao.mapper.file.PictureMapper;
import cn.hn3l.agriculture.dao.mapper.goods.GoodsMapper;
import cn.hn3l.agriculture.dao.mapper.goods.ProductorMapper;
import cn.hn3l.agriculture.entitis.behoof.Crop;
import cn.hn3l.agriculture.entitis.behoof.Sympton;
import cn.hn3l.agriculture.entitis.goods.Goods;
import cn.hn3l.agriculture.entitis.goods.GoodsType;
import cn.hn3l.agriculture.entitis.pictures.Picture;
import cn.hn3l.agriculture.exception.SaveException;
import cn.hn3l.agriculture.services.interfaces.FileService;
import cn.hn3l.agriculture.services.interfaces.goods.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Wang Weiwei
 * @Since 16-10-18
 * @Describe
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private ProductorMapper productorMapper;

    @Resource
    private EffectMapper effectMapper;

    @Resource
    private PictureMapper pictureMapper;

    @Resource
    private FileService fileService;
    public List<Goods> getPageWithGoods(int page) {
        List<Goods> goodsList = goodsMapper.getGoodsInfo();
        //为子list去重
        return goodsList;
    }

    @Override
    public Goods findGoodsById(int id) {
        return goodsMapper.findGoodsById(id);
    }

    @Override
    public List<GoodsType> findAllGoodsType() {
        List<GoodsType> goodsTypeList =  goodsMapper.findAllGoodsType();
        for (GoodsType goodsType : goodsTypeList){
            //遍历集合,拿到所有当前总类中的自类
            goodsType.setGoodsTypes(goodsMapper.
                    findGoodsTypeByPrimaryTypeId(goodsType.getId()));
        }
        return  goodsTypeList;
    }

    @Override
    public List<String> searchProductorsByName(String[] split) {
        List<String> productorList = null;
        productorList = productorMapper.searchProductorsByName(split);
        return  productorList;
    }

    @Override
    public String toSercherProviderJSONString(List<String> productorList) {
        StringBuffer buffer = new StringBuffer("[");
        for (int i = 0; i < productorList.size(); i++) {
            buffer.append("{");
            buffer.append("\"word\":");
            buffer.append("\"" + productorList.get(i) + "\"");
            buffer.append("}");
            if (i < productorList.size() - 1){
                buffer.append(",");
            }
        }
        buffer.append("]");
        return buffer.toString();
    }

    @Override
    public String saveGoodsAndCropsAndSmptonsAndPicture(
            Goods goods, List<Crop> crops, List<Sympton> symptons)
            throws Exception {
        //规范化参数
        normalizedParameterCrops((ArrayList<Crop>) crops);
        normalizedParameterSymptons((ArrayList<Sympton>) symptons);
        savaGoodsBaseAttr(goods);
        saveRGoodsToCrops(goods, (ArrayList<Crop>) crops);
        saveRGoodsToSympton(goods, (ArrayList<Sympton>) symptons);
        saveGoodsPictures(goods);
        return result;
    }

    private void saveGoodsPictures(Goods goods) {
        for (Picture picture : goods.getPictures()){
            pictureMapper.saveRGoodsToPicture(new int[]{goods.getId(),
                    pictureMapper.findIdBySrc(picture.getSrc()),1});
        }
        for (Picture picture : fileService.parseHTMLToPictureList(goods.getDescribe())){
            pictureMapper.saveRGoodsToPicture(new int[]{goods.getId(),
                    pictureMapper.findIdBySrc(picture.getSrc()),0});
        }
    }

    private void saveRGoodsToSympton(Goods goods, ArrayList<Sympton> symptons) throws SaveException {
        try{
            for (Sympton sympton : symptons){
                effectMapper.saveRGoodsToSympton(new int[]{goods.getId(),sympton.getId()});
            }
        }catch (Exception e){
            throw new SaveException("商品适用症状错误!");
        }
    }

    private String result = "1";

    private void saveRGoodsToCrops(Goods goods, ArrayList<Crop> crops) throws Exception {
        //保存适用作物与商品的对应关系
        try {
            for (Crop crop: crops){
                effectMapper.saveRGoodsToCrop(new int[]{goods.getId(),crop.getId()});
            }
        }catch (Exception e){
            throw new SaveException("商品适用作物错误!");
        }
    }

    private void savaGoodsBaseAttr(Goods goods) throws SaveException {
        try {
            goods.setId(goodsMapper.getNowId() + 1);
            //设置生产厂商的id
            goods.getProductor().setId(
                    productorMapper.findIdByName(goods.getProductor().getName()).get(0));
            goodsMapper.save(goods);
        }catch (Exception e){
            throw new SaveException("商品基本信息填写有误");
        }
    }

    private void normalizedParameterSymptons(ArrayList<Sympton> symptons) {
        for (int i = 0; i < symptons.size(); i++) {
            if (symptons.get(i).getId() < 1){
                symptons.remove(i);
            }
        }
    }

    private void normalizedParameterCrops(ArrayList<Crop> crops) {
        for (int i = 0; i < crops.size(); i++) {
            if (crops.get(i).getId() < 1){
                crops.remove(i);
            }
        }
    }
}